package programm;

import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;

import jniGlue.Librarylinker;

import soundChaser.*;

public class V_audioPlayer extends JPanel implements ActionListener
{
	// Vorschlag: kleine Änderungen sind nicht sichtbar, weil 25 auf 30 nix
	// passiert
	// doppelter DMX Bereich, wenn z.B. Rot und Rot-Blau benutzt wird
	// oder Rot und Strobe Rot
	private Librarylinker fx5;

	JButton btnSelectmp3;
	JButton btnStartplayback;
	JButton btnStopplayback;

	JLabel lblWaitingSelected;
	JLabel lblWaitingCreated;
	JLabel lblWaitingRead;
	JLabel lblWaitingComputation;

	JCheckBox chckbxFileselected;
	JCheckBox chckbxWavecreated;
	JCheckBox chckbxWaveread;
	JCheckBox chckbxComputation;

	// Audio Variables
	AudioPlayer player;
	AudioAnalyzer audioAna;
	C_audioPlayer control;

	/**
	 * Create the panel.
	 */
	public V_audioPlayer(final Librarylinker currentos)
	{
		fx5 = currentos;

		setLayout(new BorderLayout(0, 0));

		JPanel statuspanel = new JPanel();
		add(statuspanel, BorderLayout.CENTER);
		statuspanel.setLayout(new BorderLayout(0, 0));

		JPanel panelchbx = new JPanel();
		statuspanel.add(panelchbx, BorderLayout.CENTER);
		panelchbx.setLayout(new GridLayout(0, 2, 0, 0));

		chckbxFileselected = new JCheckBox("FileSelected");
		panelchbx.add(chckbxFileselected);
		chckbxFileselected.setEnabled(false);

		lblWaitingSelected = new JLabel("Loading...");
		panelchbx.add(lblWaitingSelected);
		lblWaitingSelected.setIcon(new ImageIcon(V_audioPlayer.class
				.getResource("/Images/HAPPI_Loading.gif")));
		lblWaitingSelected.setVisible(false);

		chckbxWavecreated = new JCheckBox("WaveCreated");
		panelchbx.add(chckbxWavecreated);
		chckbxWavecreated.setEnabled(false);

		lblWaitingCreated = new JLabel("Loading...");
		panelchbx.add(lblWaitingCreated);
		lblWaitingCreated.setIcon(new ImageIcon(V_audioPlayer.class
				.getResource("/Images/HAPPI_Loading.gif")));
		lblWaitingCreated.setVisible(false);

		chckbxWaveread = new JCheckBox("WaveRead");
		panelchbx.add(chckbxWaveread);
		chckbxWaveread.setEnabled(false);

		lblWaitingRead = new JLabel("Loading...");
		panelchbx.add(lblWaitingRead);
		lblWaitingRead.setIcon(new ImageIcon(V_audioPlayer.class
				.getResource("/Images/HAPPI_Loading.gif")));
		lblWaitingRead.setVisible(false);

		chckbxComputation = new JCheckBox("ComputationDone");
		panelchbx.add(chckbxComputation);
		chckbxComputation.setEnabled(false);

		lblWaitingComputation = new JLabel("Loading...");
		panelchbx.add(lblWaitingComputation);
		lblWaitingComputation.setIcon(new ImageIcon(V_audioPlayer.class
				.getResource("/Images/HAPPI_Loading.gif")));
		lblWaitingComputation.setVisible(false);

		JPanel panelwaiting = new JPanel();
		statuspanel.add(panelwaiting, BorderLayout.EAST);

		JLabel lblProgress = new JLabel("ActualProgress:");
		statuspanel.add(lblProgress, BorderLayout.NORTH);
		lblProgress.setHorizontalAlignment(SwingConstants.LEFT);

		JPanel buttonpanel = new JPanel();
		add(buttonpanel, BorderLayout.SOUTH);
		buttonpanel.setLayout(new GridLayout(0, 2, 0, 0));

		btnSelectmp3 = new JButton("SelectMP3");
		btnSelectmp3.setFont(new Font("Dialog", Font.BOLD, 14));
		btnSelectmp3.addActionListener(this);
		btnSelectmp3.setIcon(new ImageIcon(V_audioPlayer.class
				.getResource("/Images/Fairytale_fileopen.png")));
		buttonpanel.add(btnSelectmp3);

		btnStartplayback = new JButton("Start");
		btnStartplayback.setFont(new Font("Dialog", Font.BOLD, 14));
		btnStartplayback.addActionListener(this);
		btnStartplayback.setIcon(new ImageIcon(V_audioPlayer.class
				.getResource("/Images/Fairytale_player_play.png")));
		btnStartplayback.setEnabled(false);
		buttonpanel.add(btnStartplayback);

		btnStopplayback = new JButton("Stop");
		btnStopplayback.setFont(new Font("Dialog", Font.BOLD, 14));
		btnStopplayback.addActionListener(this);
		btnStopplayback.setIcon(new ImageIcon(V_audioPlayer.class
				.getResource("/Images/Gtk-media-stop.png")));
		btnStopplayback.setEnabled(false);
		buttonpanel.add(btnStopplayback);

		JLabel lblHeadline = new JLabel("PJ-DMX: Sound2Light");
		lblHeadline.setHorizontalAlignment(SwingConstants.CENTER);
		lblHeadline.setFont(new Font("Dialog", Font.BOLD, 16));
		add(lblHeadline, BorderLayout.NORTH);

	}

	public void actionPerformed(ActionEvent event)
	{
		if (event.getSource() == btnSelectmp3)
		{
			System.out.println("Button gedrückt");
			btnSelectmp3.setEnabled(false);
			btnStartplayback.setEnabled(false);
			btnStopplayback.setEnabled(false);
			
			control = new C_audioPlayer(this, fx5);
			control.start();
		} 
		else if (event.getSource() == btnStartplayback)
		{
			control.playerstart();
		} 
		else if (event.getSource() == btnStopplayback)
		{
			control.playerstop();
		}

	}
}
